<template>
	<view class="">
		<view class="form-content">
			<view v-show="!show">
				<view class="form-title">填写信息</view>
				<u-form labelPosition="left" labelWidth="300rpx" :model="form" ref="form1" errorType="toast">
					<view class="form-box w-full mt-30">
						<u-form-item label="渠道名称" prop="name" borderBottom>
							<u-input :inputAlign="inputAlign" v-model="form.name" border="none"
								placeholder="请输入渠道名称"></u-input>
						</u-form-item>
						<u-form-item label="手机号" prop="tel" borderBottom>
							<u-input type="number" :inputAlign="inputAlign" v-model="form.tel" border="none"
								placeholder="请输入手机号"></u-input>
						</u-form-item>
						<u-form-item borderBottom label="城市选择" prop="address_city"
							@click="pickerShow = true; hideKeyboard()">
							<u-icon name="arrow-right"
								:label="form.address_city != '' ? form.address_city : '请选择省/市/区县'"
								labelPos="left"></u-icon>
						</u-form-item>
						<u-form-item label="地址" prop="address_detail" borderBottom>
							<u-input :inputAlign="inputAlign" v-model="form.address_detail" border="none"
								placeholder="请输入"></u-input>
						</u-form-item>
						<u-form-item label="选择位置" prop="address" @click="getAddress();hideKeyboard()" borderBottom>
							<u-icon name="arrow-right" :label="form.address != '' ? form.address : '请选择选择位置'"
								labelPos="left"></u-icon>
						</u-form-item>
					</view>
				</u-form>
				<view class="p-30 flex-row">
					<!-- {{columns}} -->
					<u-button type="primary" text="确定申请" customStyle="margin-top: 50px" @click="submit"></u-button>
				</view>
			</view>
			<view class="flex-column align-center justify-center" v-show="show">
				<template v-if="info.status == 1">
					<view class="flex-column align-center justify-center">
						<u-icon name="checkmark-circle" size="60" :color="$u.color.success"></u-icon>
						<view class="mt-30 text-center">审核成功</view>
					</view>
					<view class="mt-30">
						<view class="flex-row">
							<view class="label">
								名称：
							</view>
							<view class="">
								{{info.name}}
							</view>
						</view>
						<view class="flex-row mt-10">
							<view class="label">
								电话：
							</view>
							<view class="">
								{{info.tel}}
							</view>
						</view>
						<view class="flex-row mt-10">
							<view class="label">
								地址：
							</view>
							<view class="">
								{{info.address_detail}}
							</view>
						</view>
						<view class="flex-row mt-10">
							<view class="label">
								地区：
							</view>
							<view class="">
								{{info.address_city}}
							</view>
						</view>
					</view>
				</template>
				<template v-else-if="info.status == 2">
					<view class="flex-column align-center justify-center">
						<u-icon name="close-circle" size="60" :color="$u.color.error"></u-icon>
						<view class="mt-30 text-center">审核拒绝</view>
					</view>
					<view class="mt-30">
						<view class="flex-row mt-10">
							<view class="label">
								拒绝原因：
							</view>
							<view class="">
								{{info.reason}}
							</view>
						</view>
					</view>
					<view class="mt-30">
						<u-button type="primary" @click="show = false">重新提交</u-button>
					</view>
				</template>
				<template v-else-if="info.status == 0">
					<view class="flex-column align-center justify-center">
						<u-icon name="minus-circle" size="60" :color="$u.color.primary"></u-icon>
						<view class="mt-30 text-center">审核中，请耐心等待...</view>
					</view>
				</template>
			</view>
		</view>
		<u-picker :show="pickerShow" ref="uPicker" :loading="pickerLoading" :columns="columns" @change="changeHandler"
			keyName="name" @confirm="pickerConfirm" @cancel="pickerShow = false"></u-picker>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	import {
		apply,
		getstate
	} from '@/common/api.js'
	import commonMixin from '@/mixins/common.js';
	export default {
		mixins: [commonMixin],
		data() {
			return {
				show: true,
				info:{},
				inputAlign: 'right',
				pickerShow: false,
				pickerLoading: false,
				form: {
					name: "",
					address_city: "",
					province: "",
					city: "",
					district: "",
					address: "",
					address_detail: "",
					longitude: "",
					latitude: "",
					tel: ""
				},
				rules: {
					name: {
						type: 'string',
						required: true,
						message: '请填写渠道名称',
						trigger: ['blur']
					},
					address_city: {
						type: 'string',
						required: true,
						message: '请填写城市选择',
						trigger: ['blur']
					},
					// 字段名称
					tel: [{
							required: true,
							message: '请输入手机号',
							trigger: ['change', 'blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					]
				},
			}
		},
		computed: {
			...mapState(["areaData"]),
			columns() {
				return this.areaData;
			}
		},
		onLoad() {
			getstate().then(res => {
				console.log('resres',res);
				if(res.data) {
					this.show = true
					this.info = res.data
				}else {
					this.show = false
				}
			})
		},
		onReady() {
			// 如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则
			this.$refs.form1.setRules(this.rules)
		},
		methods: {

			pickerConfirm(e) {
				this.form.address_city = e.value[0]['name'] + '/' + e.value[1]['name'] + '/' + e.value[2]['name']
				this.form.province = e.value[0]['value']
				this.form.city = e.value[1]['value']
				this.form.district = e.value[2]['value']
				this.pickerShow = false
			},
			getAddress() {
				let vm = this;
				uni.chooseLocation({
					success: function(res) {
						console.log(res);
						if (res.errMsg == "chooseLocation:ok") {
							vm.form.latitude = res.latitude
							vm.form.longitude = res.longitude
							vm.form.address = res.address
						}
					},
					fail(err) {
						console.log(err);
					}
				});
			},
			// 回调参数为包含columnIndex、value、values
			submit() {
				// 如果有错误，会在catch中返回报错信息数组，校验通过则在then中返回true
				this.$refs.form1.validate().then(res => {

					apply(this.form).then(res => {
						console.log('apply', res);
					})
				}).catch(errors => {
					console.log('errors', errors);
				})
			},
			hideKeyboard() {
				uni.hideKeyboard()
			}
		},
	}
</script>


<style>
	page {
		background: #F7F7F7
	}
</style>

<style lang="scss" scoped>
	/deep/ .input-placeholder {
		text-align: right;
	}

	.wborder {
		border: 1rpx solid #999;
	}

	.form-content {
		margin-top: 60rpx;
	}

	.form-box {
		background-color: #fff;
		padding: 0 30rpx;
		box-sizing: border-box;
	}

	.form-title {
		font-weight: 700;
		font-size: 38rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 0 30rpx;
	}

	.form-title::before {
		content: "";
		height: 50rpx;
		width: 6rpx;
		border-radius: 10rpx;
		background-color: #3c9cff;
		background-size: 100% 100%;
		margin-right: 10rpx;
	}

	.sub-title {
		font-weight: 700;
		font-size: 38rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 0 30rpx;
	}

	.sub-title::before {
		content: "";
		height: 40rpx;
		width: 40rpx;
		border-radius: 50%;
		background: url($label2) no-repeat;
		background-size: 100% 100%;
		margin-right: 10rpx;
	}

	.table-header {
		margin: 0 20rpx 20rpx 20rpx;
		padding: 30rpx;
		background-color: #f0fffc;
	}

	.table-content {
		padding: 0 20rpx;
	}

	.table-item {
		padding: 0 30rpx 30rpx 30rpx;
		color: #555555;
		font-size: 28rpx;
		border-bottom: 1rpx solid #E6E6E6;
		margin-bottom: 30rpx;
	}

	.table-item:last-child {
		border: none;
		margin: 0;
	}
</style>